package Homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;

/*对sort.txt进行排序*/
public class work06 {
    public static void main(String[] args) {
        try{
            BufferedReader reader = new BufferedReader(new FileReader("d:\\sort.txt"));
            BufferedWriter writer = new BufferedWriter(new FileWriter("sort.txt"));
            ArrayList<String> list = new ArrayList<>();
            String line;
            while ((line = reader.readLine()) != null) {
                if(!line.trim().isEmpty()){
                    list.add(line);
                }
            }
            reader.close();
            System.out.println(list);
            //if(!list.isEmpty()){
            Collections.sort(list);
            for (String str : list) {
                writer.write(str);
                writer.newLine();
            }
            writer.close();
            System.out.println(list);
            System.out.println("排序成功");
            //}
        }catch(Exception e){
            System.out.println("发生异常: " + e.getMessage());
        }
    }
}
